/**
 *@Description 订单详情页
 *@Author zhangnf
 *@Date 2015-09-25
 */
var tsOrderDetailCtrl  = new XFApp.client();
XFApp.client.prototype.orderDetail = {
	successCode : "10000",
	page : null,
	urlItems : {
		getOrderByIdURL : basePath+"/transorder/queryById",
		saveOrderURL : basePath+"/transorder/saveTransOrder",
		sendOrderURL : basePath+"/transorder/sendTransOrder",
		arriveAppURL : basePath+"/transorder/arriveApp",
		outAppURL : basePath+"/transorder/outApp"
	},
	queryPage : function(){
		_this = this;
		if(!!_this.page){
			_this.page.page("remote",0,$('#searchForm').serialize());
		}else{
		_this.page = $("#page").page({
			pageSize : 10,
			remote: {
				url: _this.urlItems.queryByPageURL,
		        params: $('#searchForm').serialize(), 
		        success: function (data, pageIndex) {
		        	if(data.code==_this.successCode){
		        		var pageResult = data.result;
		        		if(null == data 
								|| null == data.result 
									|| null == data.result.itemList
										|| data.result.itemList.length < 1){
		        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
		        		}else{
		        			for(var index in pageResult.itemList){
		        				var element = pageResult.itemList[index];
		        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
		        			}
		        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
		        		}
		        	}else{
		        		webDialogUtil.simpleAlert(data.msg, "提示");
		        	}
		        },
		        error : function(){
					webDialogUtil.simpleAlert("数据查询失败!");
		        }
			},
			firstBtnText : '首页',
			lastBtnText : '尾页',
			prevBtnText : '上一页',
			nextBtnText : '下一页',
			showInfo : true,
			showJump : true,
			jumpBtnText : '跳转',
			showPageSizes : true,
			infoFormat : '共{pageCount}页,{total}项'
		});
		}
	},
	formValidate : function(){
		$("#editForm").validate({
	        rules: {
	        	customscodeHidden: "required",
	        	logisticsCode: "required",
	        	logisticsName: "required",
	        	logisticsNo: "required",
	        	orderNo: "required",
	        	ebpCode: "required",
	        	ebpName: "required",
	        	trafModeHidden: "required",
	        	trafName: "required",
	        	voyageNo: "required",
	        	billNo :  "required",
	        	freight: {
	        		required: true,
	        		amount:true
	        	},
	        	insuredFee: {
	        		required: true,
	        		amount:true
	        	},
	        	currencyHidden: "required",
	        	grossWeight: {
	        		required: true,
	        		number3:true
	        	},
	        	netWeight: {
	        		required: true,
	        		number3:true
	        	},
	        	packNo: {
	        		required: true,
	        		positiveInteger:true
	        	},
	        	parcelInfo: "required",
	        	goodsInfo: "required",
	        	consingeeCtyHidden: "required",
	        	consingee: "required",
	        	consingeeAdd: "required",
	        	consingeeTel: "required",
	        	shipperCtyHidden: "required",
	        	shipper: "required",
	        	shipperAdd: "required",
	        	shipperTele: "required",
	        	toRemark: "required",
	        	stockRemark: "required",
	        	stockOutRemark: "required"
			  },
			 messages: {
				 	customscodeHidden: "请填写主管海关代码",
		        	logisticsCode: "请填写物流企业代码",
		        	logisticsName: "请填写物流企业名称",
		        	logisticsNo: "请填写物流运单编号",
		        	orderNo: "请填写订单编号",
		        	ebpCode: "请填写电商平台代码",
		        	ebpName: "请填写电商平台名称",
		        	trafModeHidden: "请填写运输方式",
		        	trafName: "请填写运输工具名称",
		        	voyageNo: "请填写航班航次号",
		        	billNo: "请填写航班航次号",
		        	freight: {
		        		required: "请填写运费"
		        	},
		        	insuredFee: {
		        		required: "请填写保价费"
		        	},
		        	currencyHidden: "请填写币制",
		        	grossWeight: {
		        		required: "请填写毛重",
		        		amount:"请填写整数或小数,例如:99.89"
		        	},
		        	netWeight: {
		        		required: "请填写净重",
		        		amount:"请填写整数或小数,例如:99.89"
		        	},
		        	packNo: {
		        		required: "请填写件数"
		        	},
		        	parcelInfo: "请填写包裹单信息",
		        	goodsInfo: "请填写商品信息",
		        	consingeeCtyHidden: "请填写收货人所在国",
		        	consingee: "请填写收货人名称",
		        	consingeeAdd: "请填写收货人地址",
		        	consingeeTel: "请填写收货人电话",
		        	shipperCtyHidden: "请填写发货人所在国",
		        	shipper: "请填写发货人名称",
		        	shipperAdd: "请填写发货人地址",
		        	shipperTele: "请填写发货人电话",
		        	toRemark: "请填写运单备注",
		        	stockRemark: "请填写运抵备注",
		        	stockOutRemark: "请填写离境备注"
			  }
	    });
		$("#editForm").validate();
	},
	saveForm : function(){
		_this = this;
		$.ajax( {
		    url:_this.urlItems.saveOrderURL,
		    data:$('#editForm').serialize(),  
		    type:'post',  
		    cache:false,  
		    dataType:'json',
		    success:function(data) {
				webDialogUtil.simpleAlert(data.msg);
		     },
		     error : function() {
				 webDialogUtil.simpleAlert("操作失败!");
		     }
		});
	},
	sendForm : function(){
		_this = this;
		$.ajax( {
			url:_this.urlItems.sendOrderURL,
			data:$('#editForm').serialize(),
			type:'post',
			cache:false,
			dataType:'json',
			success:function(data) {
				webDialogUtil.simpleAlert(data.msg);
				if(data.isSuccess){
					_this.refreshOrderId(data.result);
		    	}
			},
			error : function() {
				webDialogUtil.simpleAlert("操作失败!");
			}
		});
	},
	arriveApp : function(){
		_this = this;
		$.ajax( {
			url:_this.urlItems.arriveAppURL,
			data:$('#editForm').serialize(),
			type:'post',
			cache:false,
			dataType:'json',
			success:function(data) {
				_this.refreshOrderId(data.result);
				webDialogUtil.simpleAlert(data.msg);
			},
			error : function() {
				webDialogUtil.simpleAlert("操作失败!");
			}
		});
	},
	outApp : function(){
		_this = this;
		$.ajax( {
			url:_this.urlItems.outAppURL,
			data:$('#editForm').serialize(),
			type:'post',
			cache:false,
			dataType:'json',
			success:function(data) {
				_this.refreshOrderId(data.result);
				webDialogUtil.simpleAlert(data.msg);
			},
			error : function() {
				webDialogUtil.simpleAlert("操作失败!");
			}
		});
	},
	refreshOrderId : function(order){
		if(null == order){
			return;
		}
		$("#editForm #docketStatus").text(dksCtrl.dockerstatus.get(order.docketStatus));
	},
	initStatus : function(){
		var docketStatusHidden = $("#docketStatusHidden").text();
		if(null != docketStatusHidden
				&& ""!=docketStatusHidden){
			$("#docketStatus").text(dksCtrl.dockerstatus.get(docketStatusHidden));
		}
	},
	event : function(){
		_this = this;
		
		$("#submitBtn").on("click",function(){
			if($("#editForm").validate().form()){
				tsOrderDetailCtrl.orderDetail.sendForm();
			}
		});
		
		$("#saveBtn").on("click",function(){
			if($("#editForm").validate().form()){
				tsOrderDetailCtrl.orderDetail.saveForm();
			}
		});
		$("#arriveAppBtn").on("click",function(){
			if($("#editForm").validate().form()){
				tsOrderDetailCtrl.orderDetail.arriveApp();
			}
		});
		$("#outAppBtn").on("click",function(){
			if($("#editForm").validate().form()){
				tsOrderDetailCtrl.orderDetail.outApp();
			}
		});
		//运单表单验证
		this.formValidate();
		//初始化状态
		this.initStatus();
	}
};
$.views.converters({
	ftime : function(value) {
		return  new Date(value*1000).format("yyyy-MM-dd hh:mm:ss");
	},
	fdkstatus : function(value){
		return dksCtrl.dockerstatus.get(value);
	}
});
$(function(){
	//初始化编辑数据
	//币制下拉框
	dictCtrl.dictionary.load("currency","currencyHidden");
	//收货人国家下拉框
	dictCtrl.dictionary.load("nation","consingeeCtyHidden", "consingeeCty");
	//发货人国家下拉框
	dictCtrl.dictionary.load("nation", "shipperCtyHidden", "shipperCty");
	//海关代码初始化
	dictCtrl.dictionary.load("customsCode","customscodeHidden","customscode");
	//运输方式代码初始化
	dictCtrl.dictionary.load("transferMode","trafModeHidden","trafMode");
	//初始化事件
	tsOrderDetailCtrl.orderDetail.event();
});